/******************************************************************
 *
 *    Java Lib For JavaWeb, Powered By Shenzhen WEI YI KE JI .
 *
 *    Copyright (c) 2017-2027 Digital Telemedia Co.,Ltd
 *    http://www.d-telemedia.com/
 *
 *    Package:     com.theyeasy.dvmini.service.impl
 *
 *    Filename:    ArticlesServiceImpl.java
 *
 *    Description: TODO(用一句话描述该文件做什么)
 *
 *    Copyright:   Copyright (c) 2017-2027
 *
 *    Company:     Theyeasy Telemedia Co.,Ltd
 *
 *    @author:     hm
 *
 *    @version:    1.0.0
 *
 *    Create at:   2017年11月22日 上午10:13:53
 *
 *    Revision:
 *
 *    2017年11月22日 上午10:13:53
 *        - first revision
 *
 *****************************************************************/
package com.theyeasy.dvmini.service.impl;

import java.math.BigDecimal;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.testng.util.Strings;

import com.theyeasy.dvmini.dao.LotteryLogDao;
import com.theyeasy.dvmini.enums.ActivityStatus;
import com.theyeasy.dvmini.model.Activity;
import com.theyeasy.dvmini.model.Lotterylog;
import com.theyeasy.dvmini.service.ActivityService;
import com.theyeasy.dvmini.util.CommonUtil;
import com.theyeasy.dvmini.util.DateUtil;
import com.theyeasy.dvmini.util.PageResults;
import com.theyeasy.dvmini.util.RedisUtil;
import com.theyeasy.dvmini.vo.Lotterylog_Vo;

/**
 * @ClassName ArticlesServiceImpl
 * @Description TODO(这里用一句话描述这个类的作用)
 * @author hm
 * @Date 2017年11月22日 上午10:13:53
 * @version 1.0.0
 */
@Service
public class ActivityServiceImpl implements ActivityService {

    final String ACTIVITY_SETTING = "ACTIVITY_SETTING";// 活动设置保存key

    @Autowired
    LotteryLogDao logDao;

	@Override
	public String editActivitySetting(String beginTime, BigDecimal planMny, Integer minMny, Integer maxMny,
			Integer lotteryPercent, String city, String keyword, String unLotteryReply, String finishReply,
			String lotteryTitle, String lotterySubTitle) {

        try {
            Activity activity = null;
            Integer turnid = 1;
            Integer status = ActivityStatus.未开始.getIndex();
            String activityjson = RedisUtil.get(ACTIVITY_SETTING);
            if (!Strings.isNullOrEmpty(activityjson)) {
                activityjson = CommonUtil.JsonToObject(activityjson);
                Activity redisactivity =CommonUtil.getEntity(activityjson, Activity.class);
                if (redisactivity != null && redisactivity.getTurn() != null && redisactivity.getTurn() > 0) {
                    turnid = redisactivity.getTurn();
                }
                if (redisactivity != null && redisactivity.getStatus() > 0) {
                    status = redisactivity.getStatus();
                }

            }
            java.util.Date btime = null;
            if (!Strings.isNullOrEmpty(beginTime)) {
                try {
                    btime = DateUtil.parseTime1(beginTime);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            System.out.println("btime:"+btime);
            activity = new Activity(btime, null, status, planMny, minMny, maxMny, lotteryPercent, city, keyword,
                    unLotteryReply, finishReply, lotteryTitle, lotterySubTitle, turnid);
             
            String json=CommonUtil.getJson(activity);
            System.out.println("activity json:" + json.toString());
            RedisUtil.set(ACTIVITY_SETTING, json.toString());
            return "ok";
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return "设置失败，数据异常";
        }

    }

    @Override
    public Activity getActivitySetting() {
        String activityjson = RedisUtil.get(ACTIVITY_SETTING);
        activityjson = CommonUtil.JsonToObject(activityjson);
        Activity redisactivity =CommonUtil.getEntity(activityjson, Activity.class);
        return redisactivity;
    }

    /*
     * (非 Javadoc) Description:
     * @see com.theyeasy.dvmini.service.ActivityService#updateActivityStatus(java.lang.Integer)
     */
    @Override
    public String updateActivityStatus(Integer status) {
        try {
            String activityjson = RedisUtil.get(ACTIVITY_SETTING);
            if (Strings.isNullOrEmpty(activityjson))
                return "活动不存在";
            activityjson = CommonUtil.JsonToObject(activityjson);
            Activity redisactivity =CommonUtil.getEntity(activityjson, Activity.class);
            if (redisactivity == null)
                return "活动不存在";
            if (status == ActivityStatus.已结束.getIndex() && 
                    redisactivity.getStatus()==ActivityStatus.进行中.getIndex()) {
                redisactivity.setEndTime(CommonUtil.getTime());
                redisactivity.setStatus(status);
            } else if (status == ActivityStatus.进行中.getIndex()
                    && redisactivity.getStatus()==ActivityStatus.未开始.getIndex()) {
                redisactivity.setBeginTime(CommonUtil.getTime());
                redisactivity.setStatus(status);
            }
           
            String json =CommonUtil.getJson(redisactivity);
            System.out.println("activity json:" + json.toString());
            RedisUtil.set(ACTIVITY_SETTING, json.toString());
            return "ok";
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return "修改失败,数据异常";
        }

    }

    /*
     * (非 Javadoc) Description:
     * @see com.theyeasy.dvmini.service.ActivityService#clearActivity()
     */
    @Override
    public String clearActivity() {
        try {
            String activityjson = RedisUtil.get(ACTIVITY_SETTING);
            if (Strings.isNullOrEmpty(activityjson))
                return "活动不存在";
             activityjson = CommonUtil.JsonToObject(activityjson);
             Activity activity =CommonUtil.getEntity(activityjson, Activity.class);
            if (activity == null)
                return "活动不存在";
            if (activity.getStatus() <= 0 || activity.getStatus() != ActivityStatus.已结束.getIndex()) {
                return "当前活动还未结束，需活动结束后才可重新开启";
            }
            int turnid = activity.getTurn();
            if (activity.getStatus() == ActivityStatus.已结束.getIndex()) {
                activity.setStatus(ActivityStatus.未开始.getIndex());
                activity.setTurn(turnid + 1);
                activity.setPlanMny(BigDecimal.ZERO);
            }
            String json =CommonUtil.getJson(activity);
            RedisUtil.set(ACTIVITY_SETTING, json.toString());
            return "ok";
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return "操作失败，数据异常";
        }
    }

    /*
     * (非 Javadoc) Description:
     * @see com.theyeasy.dvmini.service.ActivityService#addLotteryLog(java.lang.Integer, java.math.BigDecimal,
     * java.lang.Integer)
     */
    @Override
    public String addLotteryLog(Integer fansid, BigDecimal mny, Integer status, Integer turn) {
        Lotterylog log = new Lotterylog();
        log.setCreatedtime(CommonUtil.getTime());
        log.setFansid(fansid);
        log.setMny(mny);
        log.setStatus(status);
        log.setTurn(turn);
        int row = logDao.insertSelective(log);
        return row > 0 ? "ok" : "添加失败，数据异常";
    }

    /*
     * (非 Javadoc) Description:
     * @see com.theyeasy.dvmini.service.ActivityService#getLotteryLog(java.lang.Integer, java.lang.Integer,
     * java.lang.Integer, java.lang.String)
     */
    @Override
    public PageResults<Lotterylog_Vo> getLotteryLogList(Integer page, Integer pagesize, Integer turn, String keyword) {
        PageResults<Lotterylog_Vo> pageResults = new PageResults<>();
        List<Lotterylog_Vo> list = logDao.getLotteryLogList((page - 1) * pagesize, pagesize, turn, keyword);
        int total = logDao.getLotteryLogCount(turn, keyword);
        pageResults.setPage(page);
        pageResults.setPageSize(pagesize);
        pageResults.setRows(list);
        pageResults.setTotal(total);
        return pageResults;
    }

	@Override
	public void updateCurrentMny(BigDecimal currentMny) {
		try {
			String activityjson = RedisUtil.get(ACTIVITY_SETTING);
			if (Strings.isNullOrEmpty(activityjson))
				return;
			activityjson = CommonUtil.JsonToObject(activityjson);
			Activity redisactivity = CommonUtil.getEntity(activityjson, Activity.class);
	
			redisactivity.setCurrentMny(currentMny);
			String json = CommonUtil.getJson(redisactivity);
			System.out.println("activity json:" + json.toString());
			RedisUtil.set(ACTIVITY_SETTING, json.toString());
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}

}
